
#include "stdafx.h"

TEST(getsocknameTest, getsockname_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    int len = 11;
    
    Header header(GetCurrentThreadId(), "getsockname");
    getsockname_in_inout getsocknameIN(sock, &len);
    getsockname_in_inout getsocknameOUT;

    std::vector<char> buffer;
    Serialize(header, getsocknameIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(getsocknameOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (getsocknameIN.s_, getsocknameOUT.s_);
    if (getsocknameIN.addrlen_ && getsocknameOUT.addrlen_)
    {
        EXPECT_EQ (*getsocknameIN.addrlen_, *getsocknameOUT.addrlen_);
    }
}

TEST(getsocknameTest, getsockname_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "getsockname");
    getsockname_in_inout getsocknameIN(sock, NULL);
    getsockname_in_inout getsocknameOUT;

    std::vector<char> buffer;
    Serialize(header, getsocknameIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(getsocknameOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (getsocknameIN.s_, getsocknameOUT.s_);
    if (getsocknameIN.addrlen_ && getsocknameOUT.addrlen_)
    {
        EXPECT_EQ (*getsocknameIN.addrlen_, *getsocknameOUT.addrlen_);
    }
}

TEST(getsocknameTest, getsockname_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    int len = 11;

    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(27015);

    Header header(GetCurrentThreadId(), "getsockname");
    getsockname_out_inout getsocknameIN((SOCKADDR*)&addr, &len, 7);
    getsockname_out_inout getsocknameOUT;

    std::vector<char> buffer;
    Serialize(header, getsocknameIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(getsocknameOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    if (getsocknameIN.addr_)
    {
        EXPECT_EQ (getsocknameIN.addr_->sa_family, getsocknameOUT.addr_->sa_family);
        int cmp = strncmp (getsocknameIN.addr_->sa_data, getsocknameOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    if (getsocknameIN.addrlen_ && getsocknameOUT.addrlen_)
    {
        EXPECT_EQ (*getsocknameIN.addrlen_, *getsocknameOUT.addrlen_);
    }
    EXPECT_EQ (getsocknameIN.wsa_last_error_, getsocknameOUT.wsa_last_error_);
}

TEST(getsocknameTest, getsockname_out_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;

    Header header(GetCurrentThreadId(), "getsockname");
    getsockname_out_inout getsocknameIN(NULL, NULL, 7);
    getsockname_out_inout getsocknameOUT;

    std::vector<char> buffer;
    Serialize(header, getsocknameIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(getsocknameOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    if (getsocknameIN.addr_)
    {
        EXPECT_EQ (getsocknameIN.addr_->sa_family, getsocknameOUT.addr_->sa_family);
        int cmp = strncmp (getsocknameIN.addr_->sa_data, getsocknameOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    if (getsocknameIN.addrlen_ && getsocknameOUT.addrlen_)
    {
        EXPECT_EQ (*getsocknameIN.addrlen_, *getsocknameOUT.addrlen_);
    }
    EXPECT_EQ (getsocknameIN.wsa_last_error_, getsocknameOUT.wsa_last_error_);
}